package io.renren.modules.wms.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.wms.entity.WeiboContentEntity;
import io.renren.modules.wms.service.WeiboContentService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/**
 * 微博爬取内容表
 *
 * @author lain
 * @email 494576412@qq.com
 * @date 2019-07-09 20:40:27
 */
@Controller
@RequestMapping("/wms/weibocontent")
public class WeiboContentController {
    @Autowired
    private WeiboContentService weiboContentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("wms:weibocontent:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = weiboContentService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/funList")
    @RequiresPermissions("wms:weibocontent:list")
    @ResponseBody
    public R funList(@RequestParam Map<String, Object> params){
        params.put("weiboType", "搞笑");
        PageUtils page = weiboContentService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/motionList")
    @RequiresPermissions("wms:weibocontent:list")
    @ResponseBody
    public R motionList(@RequestParam Map<String, Object> params){
        params.put("weiboType", "情感");
        PageUtils page = weiboContentService.queryPage(params);
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    @RequiresPermissions("wms:weibocontent:info")
    public R info(@PathVariable("id") Integer id){
		WeiboContentEntity weiboContent = weiboContentService.getById(id);

        return R.ok().put("weiboContent", weiboContent);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ResponseBody
//    @RequiresPermissions("wms:weibocontent:save")
    public R save(@RequestBody WeiboContentEntity weiboContent){
		return weiboContentService.saveWeiboCont(weiboContent);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("wms:weibocontent:update")
    public R update(@RequestBody WeiboContentEntity weiboContent){
		weiboContentService.updateById(weiboContent);

        return R.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("wms:weibocontent:delete")
    public R delete(@RequestBody Integer[] ids){
		weiboContentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/logicDelete")
    @RequiresPermissions("wms:weibocontent:delete")
    public R logicDelete(@RequestBody Integer[] ids){
        return weiboContentService.removeLogicByIds(Arrays.asList(ids));
    }

    /**
     * 生成微博模板 暂不支持gif
     */
    @RequestMapping("/genFile")
    public void genFile(@RequestParam Integer[] ids, HttpServletResponse response) throws IOException{
        byte[] data = weiboContentService.genFileByWeiboIds(Arrays.asList(ids));
        if(data == null){
            return;
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"output.html\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * 自动根据算法生成模板
     */
    @RequestMapping("/genAutoTemp")
    public R genAutoTemp(){
        return weiboContentService.genAutoTemp(null);
    }

}
